/**
 * 交易查询-交易预警
 */

angular.module('inspinia').controller('tradeWarningCtrl',function(i18nService,$scope,$http,$state,$stateParams,$compile,$filter,$log,$uibModal){
    i18nService.setCurrentLang('zh-cn');  //设置语言为中文
    $scope.info={};
    $scope.infoB={};
    $scope.tradeWarning=[];
    $scope.tradeWarningMap={};
    $scope.typeList = [{text:'连续',value:'1'},{text:'累计',value:'2'}];

    $scope.getDetail=function(){
        $http.post('tradeWarning/getTradeWarningInfo',"warningCode=TRADE_ERROR",
            {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
            .success(function(data) {
                if(data.status){
                    $scope.info=data.info;
                    $scope.tradeWarning=data.info.tradeWarningList;

                    $scope.infoB=data.infoB;
                    $scope.tradeWarningMap=data.infoB.tradeWarningMap;
                }else{
                    $scope.notice(data.msg);
                }
            });
    }
    $scope.getDetail();

    $scope.delrule = function (val,item) {
        if ( $scope.tradeWarning.length===1){
            $scope.notice("最后一条交易预警不允许删除");
            return;
        }
        $scope.tradeWarning.splice(val,1);
    }

    $scope.addrule = function () {
        let ruleCon = {"minute":'',"num":'',"errCode":''};
        $scope.tradeWarning.push(ruleCon)
    }

    //重复错误码判断
    function isRepeat(arr){
        var hash = {};
        for(var i in arr) {
            if(hash[arr[i].errCode]){
                return true;
            }
            hash[arr[i].errCode] = true;
        }
        return false;
    }

    function checkNum(obj){
        var reg = /^(0|[1-9][0-9]*)$/;
        if(obj==undefined || obj==null || obj===""){
            return true;
        }else{
            if(!reg.test(obj)){
                return true;
            }
        }
        return false;
    }

    $scope.updatetradeWarning=function(){
        if (isRepeat($scope.tradeWarning)){
            $scope.notice("该刷卡交易错误预警已添加");
            return;
        }
        if($scope.tradeWarning!=null&&$scope.tradeWarning.length>0){
            for(var i=0;i<$scope.tradeWarning.length;i++){
                var item=$scope.tradeWarning[i];
                if(checkNum(item.minute) || checkNum(item.num)){
                    $scope.notice("交易错误预警阈值设置值不正确");
                    return;
                }
                if(Number(item.minute)==0 || Number(item.num)==0){
                    if(Number(item.minute)!=0 || Number(item.num)!=0){
                        $scope.notice("交易错误预警阈值设置,其中一个值为0则其他值也需要为0");
                        return;
                    }
                }
            }
        }
        if(checkNum($scope.info.cdTime)){
            $scope.notice("交易错误预警-预警缓冲时间设置值不正确");
            return;
        }

        //tradeWarningMap.posTrade
        if(checkNum($scope.tradeWarningMap.posTrade.minute) || checkNum($scope.tradeWarningMap.posTrade.num)){
            $scope.notice("刷卡交易异常预警阀值设置,值不正确");
            return;
        }
        if(Number($scope.tradeWarningMap.posTrade.minute)==0 || Number($scope.tradeWarningMap.posTrade.num)==0){
            if(Number($scope.tradeWarningMap.posTrade.minute)!=0 || Number($scope.tradeWarningMap.posTrade.num)!=0){
                $scope.notice("刷卡交易异常预警阀值设置,其中一个值为0则其他值也需要为0");
                return;
            }
        }

        //tradeWarningMap.scanTrade
        if(checkNum($scope.tradeWarningMap.scanTrade.minute) || checkNum($scope.tradeWarningMap.scanTrade.timeoutNum)
            || checkNum($scope.tradeWarningMap.scanTrade.waitNum)){
            $scope.notice("扫码交易异常预警阀值设置,值不正确");
            return;
        }
        if(Number($scope.tradeWarningMap.scanTrade.minute)==0 || Number($scope.tradeWarningMap.scanTrade.timeoutNum)==0
            || Number($scope.tradeWarningMap.scanTrade.waitNum)==0){

            if(Number($scope.tradeWarningMap.scanTrade.minute)!=0 || Number($scope.tradeWarningMap.scanTrade.timeoutNum)!=0
                || Number($scope.tradeWarningMap.scanTrade.waitNum)!=0){
                $scope.notice("扫码交易异常预警阀值设置,其中一个值为0则其他值也需要为0");
                return;
            }
        }

        //tradeWarningMap.fastTrade
        if(checkNum($scope.tradeWarningMap.fastTrade.minute) || checkNum($scope.tradeWarningMap.fastTrade.timeoutNum)
            || checkNum($scope.tradeWarningMap.fastTrade.waitNum)){
            $scope.notice("快捷交易异常预警阀值设置,值不正确");
            return;
        }
        if(Number($scope.tradeWarningMap.fastTrade.minute)==0 || Number($scope.tradeWarningMap.fastTrade.timeoutNum)==0
            || Number($scope.tradeWarningMap.fastTrade.waitNum)==0){

            if(Number($scope.tradeWarningMap.fastTrade.minute)!=0 || Number($scope.tradeWarningMap.fastTrade.timeoutNum)!=0
                || Number($scope.tradeWarningMap.fastTrade.waitNum)!=0){
                $scope.notice("快捷交易异常预警阀值设置,其中一个值为0则其他值也需要为0");
                return;
            }
        }

        if(checkNum($scope.infoB.cdTime)){
            $scope.notice("交易异常预警-预警缓冲时间设置值不正确");
            return;
        }

        //中文帮忙转换
        if($scope.info.phones.indexOf("，") != -1){
            $scope.info.phones=$scope.info.phones.replace(/，/ig,',');
        }
        if($scope.infoB.phones.indexOf("，") != -1){
            $scope.infoB.phones=$scope.infoB.phones.replace(/，/ig,',');
        }
        $scope.info.tradeWarningList=$scope.tradeWarning;
        $scope.infoB.tradeWarningMap=$scope.tradeWarningMap;
        var subInfo={"info":$scope.info,"infoB": $scope.infoB};
        var data = {param:angular.toJson(subInfo)};
        $http.post("tradeWarning/saveTradeWarningInfo",data,$scope.postCfgAllCurrency)
            .success(function (result) {
                if (result.status) {
                    $scope.notice(result.msg);
                    $scope.getDetail();
                } else {
                    $scope.notice(result.msg);
                }
            });
    };
});